package dao.impl;

import dao.BaseDao;
import dao.RoomDao;
import entity.Room;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoomDaoImpl extends BaseDao implements RoomDao {

    Room room = null;
    @Override
    public List<Room> info() {
        conn = super.getConnection();
        List<Room> list = new ArrayList<>();
        try{
            String sql = "select * from computerroom";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()){
                room = new Room();
                room.setRoomId(rs.getInt("roomId"));
                room.setRoomName(rs.getString("roomName"));
                room.setComputerNumber(rs.getInt("computerNumber"));
                room.setManager(rs.getString("manager"));
                room.setLocation(rs.getString("location"));
                room.setState(rs.getInt("state"));
                list.add(room);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int delete(int roomId) {
        int i = 0;
        conn = super.getConnection();
        try{
            String sql = "delete from computerroom where roomId = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,roomId);
            i  = pstmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int inset(Room room) {
        int i = 0;
        conn = super.getConnection();
        try{
            String sql = "INSERT INTO `microblog`.`computerroom`( `roomName`, `computerNumber`, `manager`, `location`, `state`) VALUES (?,?,?,?,?);";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, room.getRoomName());
            pstmt.setInt(2, room.getComputerNumber());
            pstmt.setString(3, room.getManager());
            pstmt.setString(4, room.getLocation());
            pstmt.setInt(5, room.getState());
            i = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
}
